package com.ken.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ken.constant.StatusConstant;
import com.ken.context.BaseContext;
import com.ken.dto.DishDTO;
import com.ken.dto.DishPageQueryDTO;
import com.ken.entity.Dish;
import com.ken.result.PageResult;
import com.ken.result.Result;
import com.ken.service.DishService;
import com.ken.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> insertDish(@RequestBody DishDTO dto){
        dishService.insertDishAndFlavor(dto);
        Long categoryId = dto.getCategoryId();
        String key="dish_"+categoryId;
        cleanCache(key);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> queryByNamePage( DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品：{}",dishPageQueryDTO);
        Page<Dish> page=dishService.query()
                .like(dishPageQueryDTO.getCategoryId()!=null,"category_id",dishPageQueryDTO.getCategoryId())
                .like(StrUtil.isNotBlank(dishPageQueryDTO.getName()),"name",dishPageQueryDTO.getName())
                .like(dishPageQueryDTO.getStatus()!=null,"status", dishPageQueryDTO.getStatus())
                .page(new Page<>(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize()));
        return Result.success(new PageResult(page.getTotal(),page.getRecords()));
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result<String> deleteByIds(String ids){
        dishService.deleteById(ids);
        cleanCache("dish_*");
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable String id){
        DishVO dishVO = BeanUtil.copyProperties(dishService.getById(id), DishVO.class);
        return Result.success(dishVO);
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> getListById(@RequestParam String categoryId){
        List<Dish> list = dishService.lambdaQuery()
                .eq(Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, StatusConstant.ENABLE).list();
        return Result.success(list);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result<String> updateById(@RequestBody DishDTO dishDTO){
        dishService.updateDishAndFlavor(dishDTO);
        cleanCache("dish_*");
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售，停售")
    public Result<String> updateById(@PathVariable String status,@RequestParam String id){
        Dish build = Dish.builder().status(Integer.valueOf(status)).build();
        build.setUpdateTime(LocalDateTime.now());
        build.setUpdateUser(BaseContext.getCurrentId());
        dishService.update(build,new LambdaQueryWrapper<Dish>().eq(Dish::getId,id));
        cleanCache("dish_*");
        return Result.success();
    }

    private void cleanCache(String pattern){
        Set keys=stringRedisTemplate.keys(pattern);
        stringRedisTemplate.delete(keys);
    }
}
